/**
 * 岗位管理API
 */
import { request } from '@/utils/request'

// 公司相关接口
export const companyApi = {
  // 获取公司列表
  getCompanyList: (params?: any) => {
    return request.get('/jobs/companies/', params)
  },

  // 获取公司详情
  getCompanyDetail: (id: number) => {
    return request.get(`/jobs/companies/${id}/`)
  },

  // 创建公司
  createCompany: (data: any) => {
    // 如果是FormData，使用upload方法设置正确的Content-Type
    if (data instanceof FormData) {
      return request.upload('/jobs/companies/', data)
    }
    return request.post('/jobs/companies/', data)
  },

  // 更新公司
  updateCompany: (id: number, data: any) => {
    // 如果是FormData，使用uploadPut方法设置正确的Content-Type
    if (data instanceof FormData) {
      return request.uploadPut(`/jobs/companies/${id}/`, data)
    }
    return request.put(`/jobs/companies/${id}/`, data)
  },

  // 删除公司
  deleteCompany: (id: number) => {
    return request.delete(`/jobs/companies/${id}/`)
  }
}

// 岗位相关接口
export const jobApi = {
  // 获取岗位列表
  getJobList: (params?: any) => {
    return request.get('/jobs/', params)
  },

  // 获取岗位详情
  getJobDetail: (id: number) => {
    return request.get(`/jobs/${id}/`)
  },

  // 创建岗位
  createJob: (data: any) => {
    return request.post('/jobs/', data)
  },

  // 更新岗位
  updateJob: (id: number, data: any) => {
    return request.put(`/jobs/${id}/`, data)
  },

  // 删除岗位
  deleteJob: (id: number) => {
    return request.delete(`/jobs/${id}/`)
  },

  // 发布岗位
  publishJob: (id: number) => {
    return request.post(`/jobs/${id}/publish/`)
  },

  // 关闭岗位
  closeJob: (id: number) => {
    return request.post(`/jobs/${id}/close/`)
  },

  // 申请岗位
  applyJob: (jobId: number, data: any) => {
    // 如果是FormData，使用upload方法设置正确的Content-Type
    if (data instanceof FormData) {
      return request.upload(`/jobs/${jobId}/apply/`, data)
    }
    return request.post(`/jobs/${jobId}/apply/`, data)
  },

  // 获取岗位推荐
  getJobRecommendations: (params?: any) => {
    return request.get('/jobs/recommendations/', params)
  },

  // 生成岗位推荐
  generateRecommendations: () => {
    return request.post('/jobs/recommendations/generate/')
  },

  // 获取岗位统计
  getJobStatistics: () => {
    return request.get('/jobs/statistics/')
  },

  // 获取详细岗位统计
  getDetailedJobStatistics: (params?: { period?: 'week' | 'month' | 'quarter' | 'year' }) => {
    return request.get('/jobs/statistics/detailed/', params)
  }
}

// 岗位申请相关接口
export const jobApplicationApi = {
  // 获取岗位申请列表
  getApplicationList: (params?: any) => {
    return request.get('/jobs/applications/', params)
  },

  // 获取岗位申请详情
  getApplicationDetail: (id: number) => {
    return request.get(`/jobs/applications/${id}/`)
  },

  // 更新岗位申请
  updateApplication: (id: number, data: any) => {
    return request.put(`/jobs/applications/${id}/`, data)
  },

  // 删除岗位申请
  deleteApplication: (id: number) => {
    return request.delete(`/jobs/applications/${id}/`)
  },

  // 审核岗位申请
  reviewApplication: (id: number, data: any) => {
    return request.post(`/jobs/applications/${id}/review/`, data)
  }
}

// 岗位查询参数类型
export interface JobQueryParams {
  page?: number
  page_size?: number
  search?: string
  job_type?: string
  work_mode?: string
  status?: string
  company?: number
  is_featured?: boolean
  salary_min?: number
  salary_max?: number
}

// 岗位申请查询参数类型
export interface JobApplicationQueryParams {
  page?: number
  page_size?: number
  search?: string
  status?: string
  job?: number
  student?: number
}

// 公司查询参数类型
export interface CompanyQueryParams {
  page?: number
  page_size?: number
  search?: string
  company_type?: string
  company_size?: string
  industry?: string
}

// 岗位表单数据类型
export interface JobFormData {
  company: number
  title: string
  job_type: string
  work_mode: string
  department?: string
  salary_min?: number
  salary_max?: number
  salary_negotiable: boolean
  work_location: string
  remote_allowed: boolean
  description: string
  requirements: string
  responsibilities: string
  benefits?: string
  required_skills?: string
  preferred_skills?: string
  education_requirement?: string
  experience_requirement?: string
  positions_available: number
  application_deadline?: string
  contact_email: string
  contact_phone?: string
  is_long_term: boolean
  valid_from?: string
  valid_until?: string
  is_featured: boolean
}

// 公司表单数据类型
export interface CompanyFormData {
  name: string
  code?: string
  company_type: string
  company_size: string
  industry: string
  website?: string
  description?: string
  address: string
  contact_person?: string
  contact_phone?: string
  contact_email?: string
  logo?: File
  founded_year?: number
  employee_count?: number
}

// 岗位申请表单数据类型
export interface JobApplicationFormData {
  job: number
  cover_letter?: string
  resume_file?: File
}

// 岗位申请审核数据类型
export interface JobApplicationReviewData {
  status: 'approved' | 'rejected'
  review_comments?: string
}

// 岗位类型定义
export interface Job {
  id: number
  company: number
  company_name: string
  title: string
  job_type: string
  work_mode: string
  department?: string
  salary_min?: number
  salary_max?: number
  salary_negotiable: boolean
  work_location: string
  remote_allowed: boolean
  description: string
  requirements: string
  responsibilities: string
  benefits?: string
  required_skills?: string
  preferred_skills?: string
  education_requirement?: string
  experience_requirement?: string
  positions_available: number
  application_deadline?: string
  contact_email: string
  contact_phone?: string
  is_long_term: boolean
  valid_from?: string
  valid_until?: string
  is_featured: boolean
  status: string
  published_by?: number
  published_at?: string
  created_at: string
  updated_at: string
}
